<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
    <meta charset="utf-8">
    <title>标签管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="shortcut icon" th:href="@{/icon/favicon.ico}">
    <link rel="shortcut icon" th:href="@{/icon/favicon.ico}">
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}" media="all">
    <style>
        .table-search-fieldset {
            margin: 0;
            border: 1px solid #e6e6e6;
            padding: 10px 20px 5px 20px;
            color: #6b6b6b;
        }
    </style>
</head>
<body class="layui-layout-body">
<div id="LAY_app">
    <div class="layui-layout layui-layout-admin">
        <!--头部导航-->
        <div th:replace="~{admin/public/header::header}"></div>
        <!--侧边菜单-->
        <div th:replace="~{admin/public/sider::sider('tag','tags')}"></div>

        <!-- 主体内容 -->
        <div class="layui-body" id="LAY_app_body" style="margin-top: -50px">
            <div class="layui-fluid">
                <div class="layui-card">
                    <fieldset class="table-search-fieldset">
                        <legend>搜索信息</legend>
                        <div style="margin: 10px 10px 10px 10px">
                            <div class="layui-form-item">
                                <div class="layui-form-item">
                                    <div class="layui-inline" style="">
                                        <label class="layui-form-label">标签</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="tag" autocomplete="off"
                                                   class="layui-input" lay-verify="required">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <button class="layui-btn layuiadmin-btn-useradmin">
                                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </fieldset>
                    <div class="layui-card-body">
                        <div style="padding-bottom: 10px;">
                            <button class="layui-btn layui-btn-danger" data-type="batchdel" id="batchDel"><i
                                    class="layui-icon layui-icon-delete"></i>批量删除
                            </button>
                            <button class="layui-btn layui-btn-normal" id="newTag"><i
                                    class="layui-icon layui-icon-add-1"></i>添加标签
                            </button>
                        </div>

                        <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
                        <script type="text/html" id="table-useradmin-webuser">
                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                                    class="layui-icon layui-icon-edit"></i>编辑</a>
                            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                                    class="layui-icon layui-icon-delete"></i>删除</a>
                        </script>
                    </div>
                </div>
            </div>
        </div>

        <!-- 辅助元素，一般用于移动设备下遮罩 -->
        <div class="layadmin-body-shade" layadmin-event="shade"></div>
    </div>
</div>

<script th:src="@{/layuiadmin/layui/layui.js}"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'layer'], function () {
        var $ = layui.$
            , table = layui.table
            , layer = layui.layer;

        $('#newTag').click(function () {
            parent.layer.open({
                type: 2,
                title: '添加标签',
                shadeClose: true,
                shade: 0.8,
                area: ['20%', '20%'],
                content: '/admin/addTag',
                zIndex: layer.zIndex,
                success: function (layero) {
                    layer.setTop(layero);
                },
                end: function () {
                    myTable.reload({
                        where:
                            {
                                'tag' : ''
                            },
                        page: {
                            curr: 1
                        }
                    });
                }
            });
        });
        //初始化表格数据
        let myTable = table.render({
            elem: "#LAY-user-manage",
            url: '/admin/tags',
            limit: 5,                       //要传向后台的每页显示条数
            page: {               //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']//自定义分页布局
                , limits: [5, 10, 15, 20, 25, 30, 35]
                , first: false //不显示首页
                , last: false //不显示尾页
            },
            cols: [
                [
                    {type: "checkbox", fixed: "left"},
                    {field: "id",  title: "ID", sort: !0},
                    {field: "tagName", title: "标签"},
                    {title: "操作", width: 150, align: "center", fixed: "right", toolbar: "#table-useradmin-webuser"}
                ]
            ],
            height: "full-220",
            text:
                {
                    none: '搜索内容为空',
                    error: '数据请求异常'
                }
        });
        //监听搜索
        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            let tag = $('input[name="tag"]').val();
            if (tag.length === 0) {
                layer.msg('必选项不能为空', {
                    icon: 5
                });
            }
            myTable.reload({
                where:
                    {
                        'tag': tag
                    },
                page: {
                    curr: 1
                }
            })
        });

        $('#batchDel').on('click', function () {
            var checkStatus = table.checkStatus('LAY-user-manage')
                , checkData = checkStatus.data; //得到选中的数据

            if (checkData.length === 0) {
                return layer.msg('请选择数据');
            }
            layer.confirm('确定删除吗？', function (index) {
                let arrays = [];
                for (let i = 0; i < checkData.length; i++) {
                    arrays[i] = checkData[i].id;
                }
                $.ajax({
                    type: 'POST',
                    url: '/admin/batchDeleteTag',
                    data:
                        {
                            'tags': arrays
                        },
                    success: function (data) {
                        let json;
                        if ((typeof data === 'string') && data.constructor === String) {
                            if (data.charAt(0) === '<') {
                                layer.msg('服务器异常，请稍后重试', {
                                    icon: 5
                                });
                                return;
                            }
                            json = JSON.parse(data);
                        } else {
                            json = data;
                        }
                        if (json.code === 1) {
                            layer.msg('删除成功', {
                                icon: 6
                            });
                            let search = $('input[name="tag"]').val();
                            myTable.reload({
                                    where: {
                                        'tag': search
                                    },
                                    page: {
                                        curr: $(".layui-laypage-em").next().html()
                                    }
                                }
                            );
                        } else if (json.code === 2) {
                            layer.msg(json.msg, {
                                icon: 5
                            });
                        } else if (json.code === 3) {
                            layer.msg(json.msg, {
                                icon: 5
                            });
                            let search = $('input[name="tag"]').val();
                            myTable.reload({
                                    where: {
                                        'tag': search
                                    },
                                    page: {
                                        curr: $(".layui-laypage-em").next().html()
                                    }
                                }
                            );
                        }
                    },
                    error: function () {
                        layer.msg('服务器异常', {
                            icon: 5
                        })
                    }
                });
            });
        });
        //工具栏事件
        table.on("tool(LAY-user-manage)", function (obj) {
            if ("del" === obj.event) layer.confirm("真的删除这一行么", function (t) {
                layer.close(t);
                $.ajax({
                    type: 'GET',
                    url: '/admin/deleteTag/' + obj.data.id,
                    success: function (data) {
                        let json;
                        if ((typeof data === 'string') && data.constructor === String) {
                            if (data.charAt(0) === '<') {
                                layer.msg('服务器异常，请稍后重试', {
                                    icon: 5
                                });
                                return;
                            }
                            json = JSON.parse(data);
                        } else {
                            json = data;
                        }
                        if (json.code === 1) {
                            layer.msg('删除成功', {
                                icon: 6
                            });
                            obj.del();
                        } else if (json.code === 2) {
                            layer.msg(json.msg, {
                                icon: 5
                            });
                        }
                    },
                    error: function () {
                        layer.msg('服务器异常', {
                            icon: 5
                        })
                    }
                });
            });
            else if ("edit" === obj.event) {
                parent.layer.open({
                    type: 2,
                    title: '编辑标签',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['20%', '20%'],
                    content: '/admin/editTag?id=' + obj.data.id + "&tagName=" + obj.data.tagName,
                    zIndex: layer.zIndex,
                    success: function (layero) {
                        layer.setTop(layero);
                    },
                    end: function () {
                        let search = $('input[name="tag"]').val();
                        myTable.reload({
                                where: {
                                    'tag': search
                                },
                                page: {
                                    curr: $(".layui-laypage-em").next().html()
                                }
                            }
                        );
                    }
                });
            }
        });
    });
</script>
</body>
</html>
